package com.zj.printdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.inuker.bluetooth.library.BluetoothClient;
import com.inuker.bluetooth.library.connect.listener.BleConnectStatusListener;
import com.inuker.bluetooth.library.connect.response.BleConnectResponse;
import com.inuker.bluetooth.library.connect.response.BleWriteResponse;
import com.inuker.bluetooth.library.model.BleGattProfile;
import com.inuker.bluetooth.library.search.SearchRequest;
import com.inuker.bluetooth.library.search.SearchResult;
import com.inuker.bluetooth.library.search.response.SearchResponse;
import com.inuker.bluetooth.library.utils.BluetoothLog;

import java.util.HashMap;
import java.util.Map;

import static com.inuker.bluetooth.library.Code.REQUEST_SUCCESS;
import static com.inuker.bluetooth.library.Constants.STATUS_CONNECTED;
import static com.inuker.bluetooth.library.Constants.STATUS_DISCONNECTED;

public class BluetoothKitTest extends AppCompatActivity {
    Map<String,SearchResult> searchResults=new HashMap<String,SearchResult>();
    String  MAC="00:15:83:00:73:D5";
    BluetoothClient mClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_kit_test);
        findViewById(R.id.button2).setOnClickListener(onClickListener);
        findViewById(R.id.button3).setOnClickListener(onClickListener);
        findViewById(R.id.button4).setOnClickListener(onClickListener);
        mClient = new BluetoothClient(this);
        mClient.registerConnectStatusListener(MAC, mBleConnectStatusListener);

    }
    public View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button2:
                    searchDevice();
                    break;
                case R.id.button3:
                    mClient.unregisterConnectStatusListener(MAC, mBleConnectStatusListener);
                    break;
                case R.id.button4:
                    mClient.disconnect(MAC);
                    break;
            }
        }
    };
    public void searchDevice() {
        SearchRequest request = new SearchRequest.Builder()
//                .searchBluetoothLeDevice(5000, 1)   // 先扫BLE设备3次，每次3s
                .searchBluetoothClassicDevice(5000) // 再扫经典蓝牙5s
//                .searchBluetoothLeDevice(2000)      // 再扫BLE设备2s
                .build();

        mClient.search(request, new SearchResponse() {
            @Override
            public void onSearchStarted() {

            }

            @Override
            public void onDeviceFounded(SearchResult device) {
                BluetoothLog.v("address:"+device.getAddress()+"  name:"+device.getName());
                searchResults.put(device.getAddress(),device);
                BluetoothLog.v("设备大小 ："+searchResults.size());
                if(device.getAddress().equals(MAC)){
                    mClient.stopSearch();
                    BluetoothLog.v("找到设备停止扫描");
                    connectMAC();
                }
//                Beacon beacon = new Beacon(device.scanRecord);
//                BluetoothLog.v(String.format("beacon for %s\n%s", device.getAddress(), beacon.toString()));
            }

            @Override
            public void onSearchStopped() {

            }

            @Override
            public void onSearchCanceled() {

            }
        });
    }
    public  void  connectMAC(){
        mClient.connect(MAC, new BleConnectResponse() {
            @Override
            public void onResponse(int code, BleGattProfile profile) {
                BluetoothLog.v("code-->"+code);
                if (code == REQUEST_SUCCESS) {
                    BluetoothLog.v("连接成功-->"+code);
                }
            }
        });
    }

    private final BleConnectStatusListener mBleConnectStatusListener = new BleConnectStatusListener() {

        @Override
        public void onConnectStatusChanged(String mac, int status) {
            if (status == STATUS_CONNECTED) {
                BluetoothLog.v("状态：已经连接-->");

            } else if (status == STATUS_DISCONNECTED) {
                BluetoothLog.v("状态：蓝牙断开-->");

            }
        }
    };
    public  void  write(){
//        mClient.write(MAC, serviceUUID, characterUUID, bytes, new BleWriteResponse() {
//            @Override
//            public void onResponse(int code) {
//                if (code == REQUEST_SUCCESS) {
//
//                }
//            }
//        });
    }
}
